/**
 * @func
 * @desc 通过id找到树的节点
 * @param { Array } tree
 * @param { String } id
 * @returns Object
 * */
export function findTreeNode(tree, id) {
  for (const node of tree) {
    if (node.id === id) {
      return node
    }
    if (node.children) {
      const found = findTreeNode(node.children, id)
      if (found) {
        return found
      }
    }
  }
}

/**
 * @func
 * @desc 打平树结构
 * @param { Array } tree
 * @returns Array
 * */
export function flatTreeNodes(tree) {
  const arr = []
  function core(tree) {
    for (const node of tree) {
      arr.push(node)
      if (node.children) {
        core(node.children)
      }
    }
  }
  core(tree)
  return arr
}
